package com.gisway.pick.tool;

import cn.hutool.core.net.NetUtil;
import cn.hutool.system.oshi.OshiUtil;
import org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper;

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class PickWindow extends JFrame implements ActionListener {

    JTextPane textPane;
    JTextPane macPane;
    JTextPane basicPane;

    public PickWindow() {

        this.setTitle("硬件信息查看器");// 标题
        Image icon = Toolkit.getDefaultToolkit().getImage("image/gisway.png");
        this.setIconImage(icon);

        int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;// 获取屏幕宽度
        int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;// 获取屏幕高度
        int winWidth = 500;//设置父窗口宽度
        int winHeight = 200;//设置父窗口高度
        int winx = (screenWidth - winWidth) / 2; //
        int winY = (screenHeight - winHeight) / 2;
        this.setBounds(winx, winY, winWidth, winHeight);//设置窗口于屏幕居中

        Box allBox=Box.createVerticalBox();//内部组件，垂直排列
        Box cpuIdBox=Box.createHorizontalBox();//内部组件，水平排列
        Box macBox=Box.createHorizontalBox();//内部组件，水平排列
        Box basicBoardBox=Box.createHorizontalBox();//内部组件，水平排列
        JLabel label = new JLabel("cpuId:");
        JLabel macLable = new JLabel("mac:");
        JLabel basicBoardLable = new JLabel("主板id:");
        //getContentPane().setLayout(null);
        textPane = new JTextPane();
        textPane.setText(OshiUtil.getProcessor().getProcessorIdentifier().getProcessorID());
        textPane.setEditable(false);
        //textPane.setBounds(winx/4, winY/4, winWidth/4, winHeight/4);
        macPane = new JTextPane();
        macPane.setText(NetUtil.getLocalMacAddress());
        macPane.setEditable(false);
        basicPane = new JTextPane();
        basicPane.setText(OshiUtil.getSystem().getBaseboard().getSerialNumber());
        basicPane.setEditable(false);


        JButton button = new JButton();
        button.setText("复制");
        button.setActionCommand("复制cpuId");
        button.addActionListener(this);

        JButton macButton = new JButton();
        macButton.setText("复制");
        macButton.setActionCommand("复制mac");
        macButton.addActionListener(this);

        JButton basicBoardButton = new JButton();
        basicBoardButton.setText("复制");
        basicBoardButton.setActionCommand("复制主板id");
        basicBoardButton.addActionListener(this);
        //button.setBounds(winx/4, winY/4, winWidth/4, winHeight/4);
        //this.add(button, BorderLayout.SOUTH);

        cpuIdBox.add(Box.createHorizontalStrut(20));
        cpuIdBox.add(label);
        cpuIdBox.add(Box.createHorizontalStrut(15));//输入框与label的横向间隔
        cpuIdBox.add(textPane);
        cpuIdBox.add(Box.createHorizontalStrut(10));//输入框与label的横向间隔
        cpuIdBox.add(button);
        cpuIdBox.add(Box.createHorizontalStrut(20));

        macBox.add(Box.createHorizontalStrut(20));
        macBox.add(macLable);
        macBox.add(Box.createHorizontalStrut(27));
        macBox.add(macPane);
        macBox.add(Box.createHorizontalStrut(10));
        macBox.add(macButton);
        macBox.add(Box.createHorizontalStrut(20));
        basicBoardBox.add(Box.createHorizontalStrut(20));
        basicBoardBox.add(basicBoardLable);
        basicBoardBox.add(Box.createHorizontalStrut(10));
        basicBoardBox.add(basicPane);
        basicBoardBox.add(Box.createHorizontalStrut(10));
        basicBoardBox.add(basicBoardButton);
        basicBoardBox.add(Box.createHorizontalStrut(20));

        allBox.add(Box.createVerticalStrut(10));
        allBox.add(cpuIdBox);
        allBox.add(Box.createVerticalStrut(10));
        allBox.add(macBox);
        allBox.add(Box.createVerticalStrut(10));
        allBox.add(basicBoardBox);
        
        this.add(allBox, BorderLayout.NORTH);

        this.setVisible(true);
    }

    //按钮触发时间函数指定要这样写
    public void actionPerformed(ActionEvent e){
        if(e.getActionCommand()=="复制cpuId"){
            // 获取系统剪贴板
            Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
            // 封装文本内容
            Transferable trans = new StringSelection(textPane.getText());
            // 把文本内容设置到系统剪贴板
            clipboard.setContents(trans, null);
        }else if(e.getActionCommand()=="复制mac"){
            Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
            // 封装文本内容
            Transferable trans = new StringSelection(macPane.getText());
            // 把文本内容设置到系统剪贴板
            clipboard.setContents(trans, null);
        } else if(e.getActionCommand()=="复制主板id"){
            Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
            // 封装文本内容
            Transferable trans = new StringSelection(basicPane.getText());
            // 把文本内容设置到系统剪贴板
            clipboard.setContents(trans, null);
        }
    }

}
